<?php
header('Content-Type: text/html; charset=utf-8');
ini_set('memory_limit','500M');
define('ROOTPATH', dirname(dirname(dirname(dirname(__FILE__)))) . '/');
define('APPPATH', ROOTPATH . "application/");
define('BASEPATH', ROOTPATH . "/system/");
define('CONFIGPATH', APPPATH . "configs/"); 
$tmp = ROOTPATH;

set_time_limit(0);
$now = time();
$db = connect_db();
$conn = $db->linkid;
//$sql="select * from push_message where status = 1 and  starttime - $now >=0 and starttime - $now < 60";
$sql="select * from push_message where status = 1";
$result=mysql_query($sql,$conn);
if (mysql_num_rows($result) > 0) {
	while($rows=mysql_fetch_array($result)) {
		$rowsdata[] = $rows;
	}
	$sql="update push_message set status = 2  where status = 1 and  starttime - $now >=0 and starttime - $now < 60";
	$res=mysql_query($sql,$conn);
	clear_tmp_token($conn);
}
foreach ($rowsdata as $rows)
{
	$sql = '';
	switch($rows['type']) {
		case 1://玩家
			$sql = "insert tmp_token (token) SELECT token FROM tt_role_token WHERE area = '".$rows['area_ids']."' and roleid in(".$rows['role_ids'].")";
			break;
		case 2://服务器
			$sql = "insert tmp_token (token) SELECT token FROM tt_role_token WHERE area in(".$rows['area_ids'].")";
			break;
		case 3://excel
			$excelarray =  readExcel($rows['excelfile']);
			if (empty($excelarray)) {
				break;
			}
			for($i=2;$i<=count($excelarray);$i++) {
				$awardarray['areaId']          =$excelarray[$i]['A'];	//区号
				$awardarray['role_name']       =$excelarray[$i]['B'];	//角色名
				$awardarray['role_id']         =$excelarray[$i]['C'];	//角色id
				$sql = "insert tmp_token (token) SELECT token FROM tt_role_token WHERE area = '".$awardarray['areaId']."' and roleid in(".$awardarray['role_id'].")";
				$insertRes = $db->query($sql);
			}
			$sql = '';
			break;
		case 4://渠道
			if ($rows['channel'] == 'All') {
				$sql = "insert tmp_token (token) SELECT token FROM tt_role_token";
			}
			else {
				$sql = "insert tmp_token (token) SELECT token FROM tt_role_token where channel = ".$rows['channel'];
			}
			break;
		default:
			echo '未知类型';
	}
	if (!empty($sql)) {
		$insertRes = $db->query($sql);
	}
	include_once 'fsocket.php';
	$pushByToken = new Push_By_Token_Controller($db);
	$pushByToken->push($rows['content']);
}



function connect_db() {
	include_once CONFIGPATH.'db.php';
	include_once BASEPATH.'libraries/database.php';
	$db = new Database($config['db']);
	return $db;
}

function nlog($data, $filename) {
	//return ;
	if (is_array($data)) {
		$log = date("Y-m-d H:i:s") . " " . http_build_query($data) . "\r\n";
	} else {
		$log = date("Y-m-d H:i:s") . " " . $data . "\r\n";
	}
	$path = dirname(__FILE__) .'/'. $filename .date("Y-m-d"). ".txt";
	file_put_contents($path, $log, FILE_APPEND);
}

function clear_tmp_token($conn) {
	$sql = "truncate table tmp_token";
	return mysql_query($sql,$conn);;
}

function readExcel($filePath) {
	require_once(BASEPATH . 'PHPExcel/PHPExcel.php');

	$PHPExcel = new PHPExcel();

	/*     * 默认用excel2007读取excel，若格式不对，则用之前的版本进行读取 */
	$PHPReader = new PHPExcel_Reader_Excel2007();
	if (!$PHPReader->canRead($filePath)) {
		$PHPReader = new PHPExcel_Reader_Excel5();
		if (!$PHPReader->canRead($filePath)) {
			echo 'no Excel';
			return;
		}
	}

	$PHPExcel = $PHPReader->load($filePath);
	/*     * 读取excel文件中的第一个工作表 */
	$currentSheet = $PHPExcel->getSheet(0);
	//print_r($currentSheet);exit();
	/*     * 取得最大的列号 */
	$allColumn = $currentSheet->getHighestColumn();
	/*     * 取得一共有多少行 */
	$allRow = $currentSheet->getHighestRow();
	/*     * 从第二行开始输出，因为excel表中第一行为列名 */
	for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
		/*         * 从第A列开始输出 */
		for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
			$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue(); /*             * ord()将字符转为十进制数 */
			//echo iconv('utf-8','gb2312',$val)."***";
			$excelarray[$currentRow][$currentColumn] = $val;
		}
		//echo "</br></br>";
	}
	//print_r($excelarray);
	return $excelarray;
}

function set_break($tag = 'default') {
	return 0;
}
function get_break($tag = 'default') {
	return 0;
}

function get_int_type() {
	
}
?>